package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Xypjjl;
import com.corpcredit.service.GenericManager;

public class XypjjlForm extends BasePage implements Serializable {
    private GenericManager<Xypjjl, Long> xypjjlManager;
    private Xypjjl xypjjl = new Xypjjl();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setXypjjlManager(GenericManager<Xypjjl, Long> xypjjlManager) {
        this.xypjjlManager = xypjjlManager;
    }

    public Xypjjl getXypjjl() {
        return xypjjl;
    }

    public void setXypjjl(Xypjjl xypjjl) {
        this.xypjjl = xypjjl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        xypjjlManager.remove(xypjjl.getId());
        addMessage("xypjjl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            xypjjl = xypjjlManager.get(id);
        } else {
            xypjjl = new Xypjjl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (xypjjl.getId() == null || xypjjl.getId() == 0);
        xypjjl.setJbqk(jbqk);
        // set username who act this operation, get the user name from httprequest
        xypjjl.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        xypjjl.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        xypjjl.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        xypjjlManager.save(xypjjl);

        String key = (isNew) ? "xypjjl.added" : "xypjjl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            // set the id to the next page
            this.id = xypjjl.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 